package org.xcorpio.java8.chapter12;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

public class ManipulatingLocalDateTime {

    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2014, 3, 18);
        System.out.println("base:" + date);
        System.out.println(System.identityHashCode(date));
        LocalDate withYear = date.withYear(2015);
        System.out.println(withYear);
        System.out.println(System.identityHashCode(withYear));
        LocalDate with = date.with(ChronoField.DAY_OF_MONTH, 3);
        System.out.println(with);
        LocalDate plusDays = date.plusDays(1);
        System.out.println(plusDays);
        LocalDate minus = date.minus(2, ChronoUnit.YEARS);
        System.out.println(minus);
        LocalDate temporalAdjuster = date.with(new NextWorkingDay());
        System.out.println(temporalAdjuster);

        // date time parsing and formating
        System.out.println();
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));
        System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

        System.out.println(LocalDate.parse("20111111", DateTimeFormatter.BASIC_ISO_DATE));
        System.out.println(LocalDate.parse("2011-11-11", DateTimeFormatter.ISO_DATE));

        // Creating a DateTimeFormatter from a pattern
        System.out.println();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate date1 = LocalDate.of(2016, 11, 12);
        String formattedDate = date1.format(formatter);
        System.out.println(formattedDate);
        System.out.println(LocalDate.parse(formattedDate, formatter));

        // creating a localized DateTimeFormatter
        DateTimeFormatter italianFormatter = DateTimeFormatter.ofPattern("d.MMMM.yyyy", Locale.ITALIAN);
        LocalDate now2 = LocalDate.now();
        System.out.println(now2.format(italianFormatter));
    }
}
